package com.mccken.demo.io.netty.codec;

import java.util.List;

import com.mccken.demo.io.netty.support.BaseRpcBean;
import com.mccken.demo.io.netty.util.HessianSerializer;
import com.mccken.demo.io.netty.util.MySerializer;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ReplayingDecoder;

/**
 * @program: mccken-collect
 * @description:
 * @author: mccken
 * @create: 2022-04-30 18:32
 **/
public class MyDecoder<T extends BaseRpcBean> extends ReplayingDecoder<Void> {

	private static MySerializer hessianSerializer = new HessianSerializer();
	
	private Class<T> clazz;
	
	public MyDecoder(Class<T> clazz) {
		this.clazz = clazz;
	}
	
	@Override
	protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {

		byte[] bytes = new byte[in.readInt()];
		in.readBytes(bytes);

		BaseRpcBean rpcBean = hessianSerializer.deseralize(bytes, clazz);
		out.add(rpcBean);
	}
}
